package com.oocl.pmsystem.db;

import com.oocl.pmsystem.bean.Command;
import com.oocl.pmsystem.bean.comparator.SuperComparator;
import com.oocl.pmsystem.bean.condition.Condition;
import com.oocl.pmsystem.bean.condition.UpdateCondition;
import com.oocl.pmsystem.model.DBConnection;

/**
 * 自定义泛型双向链表
 * 
 * @author WUJA13
 * 
 * @param <V>
 */
public class SuperLinkedList<V> {

	private int size = 0;
	private Node<V> head;
	private Node<V> tail;
	private Node<V> point;

	/**
	 * 静态内部类 目的： private 表示外部不可见，而static 表示内部类不能访问外部类的成员方法
	 * 
	 * @author WUJA13
	 * 
	 * @param <V>
	 */
	private static class Node<V> {
		V value;
		Node<V> prior;
		Node<V> next;

		public Node() {
			super();
		}

		public Node(V value) {
			this.value = value;
		}
	}

	/**
	 * 初始化 head -> last size:0
	 */
	public SuperLinkedList() {
		this.head = new Node<V>();
		this.tail = new Node<V>();
		this.head.value = null;
		this.head.prior = null;
		this.tail.value = null;
		this.tail.prior = this.head;
		this.tail.next = null;
		this.head.next = this.tail;
		this.size = 0;
	}


	/**
	 * 
	 * 增
	 * 
	 * @param val
	 *            要添加的数据
	 * @return 是否插入成功
	 */
	public boolean add(V val) {
		if (val == null)
			return false;
		Node<V> node = new Node<V>();
		node.value = val;
		this.tail.prior.next = node;
		node.prior = this.tail.prior;
		node.next = this.tail;
		this.tail.prior = node;
		this.size++;
		return true;
	}

	/**
	 * 删 根据元素的删除条件， 符合条件的就删除
	 * 
	 * @return
	 */
	public boolean delete(Condition<V> condition) {

		// //遍历所有元素
		// /如果这个元素符合这个条件
		if (this.size == 0) {
			return false;
		}
		this.point = this.head.next;
		while (this.point != this.tail) {
			if (condition.match(this.point.value)) {
				this.point.prior.next = this.point.next;
				this.point.next.prior = this.point.prior;
				this.size--;
				return true;
			}
			this.point = this.point.next;
		}
		return false;

	}

	/**
	 * 查
	 */
	public V[] query(Condition<V> condition) {
		// //遍历所有元素
		// /如果这个元素符合这个条件
		if (this.size == 0) {
			System.out.println("The list is empty!");
			return null;
		}
		if (condition == null) {
			// /全部输出
			return this.traverse();
		}
		// /打印并输入结果
		int resCount = 0;
		V[] vList = (V[]) new Object[this.size];
		this.point = this.head.next;
		while (this.point.value != null) {
			if (condition.match(this.point.value)) {
				vList[resCount++] = this.point.value;
			}
			this.point = this.point.next;
		}
		return vList;
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public boolean update(UpdateCondition<V> condition) {
		// //遍历所有元素
		// /如果这个元素符合这个条件
		if (this.size == 0) {
			System.out.println("The list is empty!");
			return false;
		}
		this.point = this.head.next;
		while (this.point != this.tail) {
			if (condition.match(this.point.value)) {
				this.point.value = condition.onUpdate(this.point.value);
				return true;
			}
			this.point = this.point.next;
		}
		return false;
	}

	/**
	 * 清空链表
	 */
	public void clear() {
		this.size = 0;
		this.head.next = this.tail;
		this.tail.prior = this.head;
	}

	/**
	 * 遍历
	 */
	private V[] traverse() {
		if (this.size == 0) {
			System.out.println("This is an empty linkedlist！");
			return null;
		}
		int resCount = 0;
		V[] vList = (V[]) new Object[this.size];
		this.point = this.head.next;
		while (this.point != this.tail) {
			vList[resCount++] = this.point.value;
			this.point = this.point.next;
		}
		return vList;
	}

	/**
	 * 从小到大排序： 采用QuickSort
	 */
	public V[] sort(SuperComparator<V> superComparator) {
		V[] resList = (V[]) new Object[this.size];
		this.point = this.head.next;
		int i = 0;
		while (this.point.value != null) {
			resList[i++] = this.point.value;
			this.point = this.point.next;
		}
		// /快排
		quickSort(resList, 0, resList.length - 1, superComparator);
		return resList;
	}

	/**
	 * 快排
	 * 
	 * @param resList
	 *            目标数组
	 * @param start
	 *            数组起点
	 * @param end
	 *            数组终点
	 * @param superComparator
	 *            元素大小比较器
	 */
	private void quickSort(V[] resList, int start, int end,
			SuperComparator<V> superComparator) {
		// TODO Auto-generated method stub
		if (start < end) {
			int p = deal(resList, start, end, superComparator);
			quickSort(resList, start, p - 1, superComparator);
			quickSort(resList, p + 1, end, superComparator);
		}
	}

	private int deal(V[] resList, int start, int end,
			SuperComparator<V> superComparator) {
		int l = start;
		int r = end;
		V temp = resList[l];
		while (l < r) {
			while (l < r && superComparator.compare(resList[r], temp) > 0)
				r--;
			if (l < r) {
				resList[l] = resList[r];
				l++;
			}
			while (l < r && superComparator.compare(resList[l], temp) < 0)
				l++;
			if (l < r) {
				resList[r] = resList[l];
				r--;
			}
		}
		resList[l] = temp;
		return l;
	}

	// /自身排序（不可恢复的排序）
	// /采用BubbleSort
	private void irreversibleSort(SuperComparator<V> superComparator) {
		System.out.println("开始sort！");
		if (this.size <= 1)
			return;
		V temp;// /辅助空间
		Node<V> cur = this.head.next;// /指向第一个元素
		Node<V> last = this.tail;
		while (cur.next != last) {// 大循环：指定冒泡终点
			while (cur.next != last) {// /小循环： 进行逐一比较（冒泡）
				if (superComparator.compare(cur.value, cur.next.value) > 0) {
					// /交换值
					temp = cur.value;
					cur.value = cur.next.value;
					cur.next.value = temp;
				}
				cur = cur.next;
			}
			last = cur;// /下一次，到上一次的cur次终止
			cur = this.head.next;
		}
	}

}
